<template>
  <div class="log_manage_container h-80%">
    <el-card>
      <template #header>
        <div class="flex justify-between items-center w-full">
          <span>日志管理</span>
          <div class="global_btn m-t-5px" style="height: 30px;" @click="handleClearRssiLog"  >
            {{ t('functionService.clear_rssi_log_label') }}
          </div>
        </div>
      </template>
      <div class="log_manage_header flex justify-between ">
      <ContainerHeader :headerList="headerList" @handleCurrentChange="handleCurrentChange" />
       <div  class="global_btn m-t-5px" style="height: 35px;" @click="handleDownloadLog" :loading="isExportLog" >
        {{ t('functionService.rssi_log_export_label') }}
       </div>
    </div>
    <keep-alive :include="includeKeys">
      <component 
      :is="activeComponent.type" 
      :key="activeComponent.key"
      :id="activeComponent.key"
      :pageType="activeComponent.pageType"
      :fileName="activeComponent.fileName"
      :logTypeLabel="logTypeLabel"
       :isFirstLoad="isFirstLoad"
      v-model:isExportLog="isExportLog"
      v-model:currentDataLength="currentDataLength"
      />
    </keep-alive>
    </el-card>  
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
  name: 'LogManage',
})
</script>
<script setup  lang="ts">
import { useI18n } from 'vue-i18n'
import LogManageInfo from './components/logManageInfo.vue'
import { ElNotificationFn } from '@/utils/computedUtils'
import { LogManageApi } from '@/api/logManage'
const currentDataLength = ref(0)
const isFirstLoad = ref(true)
const { t } = useI18n()
const headerList = computed(()=>[
  {
    name: t('functionService.ap_log_label'),
    key: 'ap_log',
  },
  {
    name: t('functionService.cp_log_label'),
    key: 'cp_log',
  },
 /*  {
    name: t('functionService.security_module_log_label'),
    key: 'security_module_log'
  }, */
  {
    name: t('functionService.kernel_log_label'),
    key: 'kernel_log',
  }
])
const components:any = [
  { type: formatComponentInstance(LogManageInfo,'ap_log'), key: 'ap_log',pageType:4,fileName:"cscn_device_ap_log"},
  { type: formatComponentInstance(LogManageInfo,'cp_log'), key: 'cp_log',pageType:7,fileName:"cscn_device_cp_log"},
  // { type: formatComponentInstance(LogManageInfo,'security_module_log'), key: 'security_module_log'},
  { type: formatComponentInstance(LogManageInfo,'kernel_log'), key: 'kernel_log',pageType:1,fileName:"cscn_device_kernel_log"},
]
const activeComponent:any= ref(components[0])
const includeKeys = computed(()=>{
  return ['ap_log','cp_log','kernel_log']
})
const handleCurrentChange = (item:any,lastIndex:number) => {
  const existingComponent = components.find((i:any)=>i.key ==item.key)
  activeComponent.value = existingComponent
  isFirstLoad.value = false
}
function formatComponentInstance (component: any, componentName: string) {
  return markRaw({  
    name: componentName,
    render: () => h(component)
  })
}
const logTypeLabel = computed(()=>{
  switch(activeComponent.value.key){
    case 'ap_log':
      return t('functionService.export_ap_log_label')
    case 'cp_log':
      return t('functionService.export_cp_log_label')
    case 'kernel_log':
      return t('functionService.kernel_log_export_label')
   }
})
const isExportLog = ref(false)
function handleDownloadLog() {
  isExportLog.value = true
}
 async function handleClearRssiLog() {
  try {
    await LogManageApi.reqClearLog({
      id:6
    })
    ElNotificationFn('success', t('functionService.clear_log_success_label'))
  } catch (error) {
    ElNotificationFn('error', t('functionService.clear_log_failure_label'))
  }
}
</script>
<style lang="scss" scoped>
@import '@/styles/card.scss';
.log_manage_container {
  height: 100%;
  display: flex;
  flex-direction: column;
  :deep(.el-card) {
    height: 100%;
    display: flex;
    flex-direction: column;
    background: transparent;
    .el-card__body {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      padding: 0 20px 0 20px;
    }
  }
  .log_manage_header {
    padding: 10px;
  }
}
.el-button {
  height: 40px;
}

</style>


